#pragma once

class Window {

	friend class Engine;
	
private:

	GLsizei width = 640;
	
	GLsizei height = 480;

	SDL_Window* window;
	
	SDL_GLContext context;
	
	void resize(GLsizei width, GLsizei height);

	void swap();
	
public:

	void setTitle(const char* title);

	const GLsizei getWidth() const;
	
	const GLsizei getHeight() const;

	Window();
	
	~Window();
	
};

void Window::swap(){
	SDL_GL_SwapWindow(window);
}

const GLsizei Window::getWidth() const{
	return width;
}

const GLsizei Window::getHeight() const{
	return height;
}

void Window::resize(GLsizei width, GLsizei height){
	Window::width = width;
	Window::height = height;
}

Window::Window(){
	if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER)){
		fprintf(stderr, "%s\n", SDL_GetError());
		exit(1);
	}
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION,4);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION,3);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,SDL_GL_CONTEXT_PROFILE_CORE);
	window = SDL_CreateWindow(
		"SDL2",
		SDL_WINDOWPOS_CENTERED,
		SDL_WINDOWPOS_CENTERED,
		width,
		height,
		SDL_WINDOW_OPENGL
	);
	if (!window) {
		fprintf(stderr, "%s\n", SDL_GetError());
		exit(1);
	}
	context = SDL_GL_CreateContext(window);
	if (!context) {
		fprintf(stderr, "%s\n", SDL_GetError());
		exit(1);
	}
	glewExperimental = 1;
	GLenum err = glewInit();
	if (GLEW_OK != err){
		fprintf(stderr, "%s\n", glewGetErrorString(err));
		exit(1);
	}
}

Window::~Window(){
	SDL_DestroyWindow(window);	
	SDL_Quit();
}

void Window::setTitle(const char* title){
	SDL_SetWindowTitle(window,title);
}
